<template>
	<view>
		<view class="fromWp">
			<u--form labelPosition="left" :model="formData" ref="form" labelWidth='180' labelAlign='left'>

				<view class="introductionWp">
					<view class="introductionposition">
						个人标签
					</view>
					<view class="personInfoTagWp">
						<view class="tagItemWp" v-for="(item, index) in  personInfoTagsFilter" :key="index">
							<u-tag :text="item.name" :name="index" plainFill plain bgColor="#e9f0fd"
								v-if="item.isDel!=1" color="#276aee" :closable='true' @close="closeTags">
							</u-tag>
						</view>
					</view>
					<view class="cusInputWp">
						<u-input maxlength="5" placeholder="请输入自定义标签(选填)" border="bottom" v-model="customerTagsVal">
							<template slot="suffix">
								<u-button @tap="addCusTags" size="mini" type="primary" plain>添加
								</u-button>
							</template>
						</u-input>
					</view>
				</view>
				<view class="introductionWp">
					<view class="introductionposition">
						个人简介/业务方向
					</view>
					<textarea auto-height v-model='introduction' :maxlength="200"
						style="background:#fff;padding: 20rpx 0;color:#333;width: 100%; " />
				</view>
				<view class="introductionWp">
					<view class="introductionposition">
						风采照片
					</view>
					<u-upload accept="image" :fileList="fileListImg" @afterRead="afterRead" @delete="deletePic"
						name="Img" :multiple='true' :maxCount="10" width="200" height="200" imageMode="aspectFit">
						<view class="uploadWp">
							<u-icon name="plus" color="#c9cdd4" size="60"></u-icon>
							<text style="color: #c9cdd4;font-size: 24rpx;">点击上传</text>
						</view>
					</u-upload>
				</view>
			</u--form>
			<view class="submitWp">
				<u-button @click="submit" color="#276aee ">创建</u-button>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters
	} from 'vuex'
	import baseConfig from '@/config/baseUrl.js'
	export default {
		data() {
			return {
				cardId: '',
				introduction: '', //个人简介
				//上传微信二维码
				fileListWx: [],
				//上传图片显示
				fileListImg: [],
				customerTagsVal: '',
				//  个人标签列表
				personInfoTags: [],
				uploadfileList: [],
				uploadfileListWx: [],

			}
		},

		onLoad(option) {
			this.cardId = option.cardId
		},

		computed: {
			...mapGetters(['userInfo']),
			personInfoTagsFilter() {
				if (this.personInfoTags.length == 0) {
					return []
				}
				return this.personInfoTags.filter(item => item.isDel !== 1)
			},
		},
		methods: {
			//添加自定义tags,自定义tags 使用默认颜色
			addCusTags() {
				if (!this.customerTagsVal) {
					return uni.showToast({
						title: '请输入自定义标签',
						icon: 'none'
					});
				}
				if (this.customerTagsVal.length > 10) {
					return uni.showToast({
						title: '标签长度不能超出10字符',
						icon: 'none'
					});
				}
				let checkedArr = this.personInfoTags.filter(item => {
					return item.checked
				})
				let item = {
					name: this.customerTagsVal,
					color: '#276AEE',
					bgColor: '#E9F0FD',
					sort: checkedArr.length ? checkedArr.length : 0,
					isDel: 0

				}
				this.personInfoTags.push(item)
				this.customerTagsVal = ''
			},
			//关闭tags
			closeTags(index) {
				let arr = this.personInfoTags.filter(item => item.isDel !== 1)
				let item = arr[index]
				item = Object.assign(item, {
					isDel: 1
				})
				this.personInfoTags.splice(index, 1, item)
			},


			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			bindTextAreaBlur(e) {
				console.log(e);
				if (!e.detail.value) {
					return
				}
				this.introduction = e.detail.value
			},
			// 新增图片
			async afterRead(event) {
				console.log('新增图片', event);
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: event.name,
						url: result,
						fileName: event.file[0].type + event.file[0].size + '',
						filePath: result
					}))
					fileListLen++
				}

			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseConfig.uploadUrl + '/casugn-system/sys/common/upload',
						filePath: url,
						name: 'file',
						success: (res) => {
							let data = JSON.parse(res.data)
							if (data.success) {
								resolve(data.message)
							}
						}
					});
				})
			},

			submit() {
				let imageList = this.fileListImg.map(item => item.url)
				imageList = imageList.join()
				// 创建名片的时候 提交标签需要过滤
				let bcCardLabelList = this.personInfoTags.filter(item => item.isDel !== 1)
				let payload = {
					imageList,
					cardId: this.cardId,
					introduction: this.introduction,
					bcCardLabelList,
				}
				this.$api.createCardIntroduction(payload).then(res => {
					if (res.success) {
						uni.showToast({
							title: '添加成功'
						})
						uni.navigateBack()
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}

				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .u-upload__success {
		display: none !important;
	}

	::v-deep .u-upload__deletable {
		right: 20upx;
		top: 20upx;
		width: 20upx;
		height: 20upx;
		text-align: center;
		border-bottom-left-radius: 0;

		// background: transparent;
		.u-icon--right {
			text {
				font-size: 30upx !important;
				top: 8upx !important;
				left: 6upx !important;
			}
		}
	}

	.fromWp {

		.formItemWp {
			margin: 0 50upx;
		}

		.uploadWp {
			width: 218rpx;
			height: 218rpx;
			background-color: #f7f8fa;
			justify-content: center;
			display: flex;
			flex-direction: column;
			align-items: center;
		}

		.personInfoTagWp {
			display: flex;
			align-items: center;
			flex-wrap: wrap;

			.tagItemWp {
				margin-right: 14upx;
				margin-bottom: 20upx;
			}
		}

		.submitWp {
			margin: 50upx;
			padding-bottom: 50upx;
		}

		.introductionWp {
			margin: 0 30upx;
			margin-top: 20rpx;

			.introductionposition {
				margin: 16rpx 0;
				height: 36rpx;
				font-size: 26rpx;
				font-weight: 400;
				color: #1D2129;
				line-height: 36rpx;
			}

			.tagsTitle2 {
				font-size: 24rpx;
				color: #666;
				padding-left: 10upx;
			}
		}
	}
</style>
